// 刷新 Token
export default defineEventHandler(async (event) => {
    try {
        // 从 Cookie 获取 refreshToken
        const refreshToken = getCookie(event, 'refresh_token')
        if (!refreshToken) {
            return unauthorizedResponse("refresh_token不能为空")
        }
        // 验证 refreshToken
        const payload = verifyRefreshToken(refreshToken)
        if (!payload) {
            return unauthorizedResponse("refresh_token无效")
        }
        // 删除 payload 中的 iat 和 exp
        delete (payload as any).iat;
        delete (payload as any).exp;
        // 生成新的 accessToken
        const newAccessToken = generateAccessToken(payload)
        return { accessToken: newAccessToken }
    } catch (error) {
        console.error('refresh error', error);
        return catchErrorResponse(error)
    }
})